package mlp.game.cherries;

import java.util.ArrayList;

import com.overkill.game.GameEngine;
import com.overkill.game.Renderable;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PointF;
import android.graphics.Paint.Style;
import android.graphics.RectF;

public class Belt {
	private float lineDistance = 150;
	
	private RectF frame;
	private ArrayList<Float> lines = new ArrayList<Float>();
	private Paint pBackground = new Paint();
	private Paint pLines = new Paint();
	private Paint pBorder = new Paint();
	
	public Belt(RectF frame) {
		lineDistance = GameEngine.dip(100);
		
		this.frame = frame;
		pBackground.setColor(0xff678794);
		pBackground.setStyle(Style.FILL);
		
		pBorder.setColor(0xff729BA1);
		pBorder.setStyle(Style.STROKE);
		pBorder.setStrokeWidth(8);
		
		pLines.setColor(0xff809DAB);
		pLines.setStyle(Style.STROKE);
		pLines.setStrokeWidth(5);
		
		for(float i = getFrame().left; i < frame.width(); i += lineDistance){
			lines.add(i);
		}
	}

	public void update(long gameTime){
		ArrayList<Float> linesToRemove = new ArrayList<Float>();
		for(int i = 0; i < lines.size(); i++){
			lines.set(i, lines.get(i) + CherryEngine.GAME_SPEED);
			if(lines.get(i) > getFrame().right){
				linesToRemove.add(lines.get(i));
			}
		}
		lines.removeAll(linesToRemove);
		if(lines.get(lines.size() - 1) > (getFrame().left + lineDistance)){
			lines.add(getFrame().left);
		}
	}
	
	public void draw(Canvas canvas){
		canvas.drawRect(getFrame(), pBackground);
		for(int i = 0; i < lines.size(); i++){
			canvas.drawLine(lines.get(i), getFrame().top, lines.get(i), getFrame().bottom, pLines);
		}
		canvas.drawLine(getFrame().left, getFrame().top, getFrame().right, getFrame().top, pBorder);
		canvas.drawLine(getFrame().left, getFrame().bottom, getFrame().right, getFrame().bottom, pBorder);
	}
	
	public RectF getFrame() {
		return frame;
	}

	public void setFrame(RectF frame) {
		this.frame = frame;
	}
	
	public boolean isHit(Renderable r){
		return RectF.intersects(getFrame(), r.getFrame());
	}
	
	public boolean isHit(float x, float y){
		return getFrame().contains(x, y) ;
	}
	
	public boolean isHit(PointF p){
		return this.isHit(p.x, p.y);
	}	
}
